package com.heu.blood.bag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.heu.blood.bag.vo.BloodBagRecycleAddVo;
import com.heu.blood.bag.vo.BloodBagRecycleSelectVo;
import com.heu.blood.common.utils.Query;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.match.dao.BloodInformationDao;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.match.exception.TimeException;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;


import com.heu.blood.bag.dao.BloodBagRecycleNewDao;
import com.heu.blood.bag.entity.BloodBagNewRecycleEntity;
import com.heu.blood.bag.service.BloodBagNewRecycleService;


@Service("bloodBagNewRecycleService")
public class BloodBagNewRecycleServiceImpl extends ServiceImpl<BloodBagRecycleNewDao, BloodBagNewRecycleEntity> implements BloodBagNewRecycleService {

    @Autowired
    private BloodInformationDao bloodInformationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BloodBagNewRecycleEntity> page = this.page(
                new Query<BloodBagNewRecycleEntity>().getPage(params),
                new QueryWrapper<BloodBagNewRecycleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils bloodBagList(BloodBagRecycleSelectVo bloodBagRecycleSelectVo) {
        // 校验时间
        if (bloodBagRecycleSelectVo.getStartTime() != null
                && bloodBagRecycleSelectVo.getEndTime() != null
                && bloodBagRecycleSelectVo.getStartTime().after(bloodBagRecycleSelectVo.getEndTime())) {
            throw new MyException(TimeException.TIME_OUTLINE);
        }
        // 分页信息先搞定
        Map<String,Object> params = new HashMap<>();
        params.put("limit", bloodBagRecycleSelectVo.getLimit());
        params.put("page", bloodBagRecycleSelectVo.getPage());
        // 下面处理筛选的信息
        LambdaQueryWrapper<BloodBagNewRecycleEntity> queryWrapper = Wrappers.lambdaQuery(BloodBagNewRecycleEntity.class);
        queryWrapper.ge(bloodBagRecycleSelectVo.getStartTime() != null, BloodBagNewRecycleEntity::getRecycleTime, bloodBagRecycleSelectVo.getStartTime())
                .le(bloodBagRecycleSelectVo.getEndTime() != null, BloodBagNewRecycleEntity::getRecycleTime, bloodBagRecycleSelectVo.getEndTime())
                .like(Strings.isNotBlank(bloodBagRecycleSelectVo.getBloodId()), BloodBagNewRecycleEntity::getBloodId, bloodBagRecycleSelectVo.getBloodId())
                .like(Strings.isNotBlank(bloodBagRecycleSelectVo.getBloodType()), BloodBagNewRecycleEntity::getBloodType, bloodBagRecycleSelectVo.getBloodType());
        queryWrapper.orderByDesc(BloodBagNewRecycleEntity::getGmtCreate);
        IPage<BloodBagNewRecycleEntity> page = this.page(
                new Query<BloodBagNewRecycleEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public int bloodBagQuickAdd(BloodBagRecycleAddVo bloodBagRecycleAddVo) {
        BloodBagNewRecycleEntity bloodBagRecycleEntity=new BloodBagNewRecycleEntity();
        String bloodId = bloodBagRecycleAddVo.getBloodId();
        LambdaQueryWrapper<BloodInformationEntity> applicationWrapper = new LambdaQueryWrapper<>();
        applicationWrapper.eq(Strings.isNotBlank(bloodId),BloodInformationEntity::getBloodId,bloodId);
        BloodInformationEntity bloodInformationEntity=bloodInformationDao.selectOne(applicationWrapper);
        if (bloodInformationEntity == null){
            return 0;
        }
        BeanUtils.copyProperties(bloodInformationEntity,bloodBagRecycleEntity);
        bloodBagRecycleEntity.setRecycleTime(new Date());
        bloodBagRecycleEntity.setGmtCreate(new Date());
        bloodBagRecycleEntity.setGmtModified(new Date());
        bloodBagRecycleEntity.setOperatorId(bloodBagRecycleAddVo.getOperatorId());
        bloodBagRecycleEntity.setOperatorName(bloodBagRecycleAddVo.getOperatorName());
        boolean save = this.save(bloodBagRecycleEntity);
        if (save){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int bloodBagSlowAdd(BloodBagNewRecycleEntity bloodBagRecycle) {
        bloodBagRecycle.setRecycleTime(new Date());
        boolean save = this.save(bloodBagRecycle);
        if (save){
            return 1;
        }else{
            return 0;
        }
    }


}